package service

import (
	"fmt"
	"scase.io/application-auto-scaling-service/pkg/api/model"
	"scase.io/application-auto-scaling-service/pkg/common"
	"scase.io/application-auto-scaling-service/pkg/utils"
	"scase.io/application-auto-scaling-service/pkg/utils/logger"
)

func (s *EventService) BuildInstanceEventReq(name, resId, detail, msg, state string) *model.CreateEventRequest {
	registerReq := &model.CreateEventRequest{
		Name:         name,
		ResourceType: common.EventResourceTypeInstance,
		TraceService: common.EventResourceTraceService,
		ResourceId:   resId,
		ResourceName: common.EventResourceTypeInstance,
		TraceState:   state,
		EventType:    common.EventTypeData,
		SourceIp:     utils.GetLocalIP(),
		Details:      detail,
		SmnMessage:   fmt.Sprintf("Have triggerred an %s event: Name: %s, Abstract:%s", state, name, msg),
	}
	return registerReq
}

func (s *EventService) RegisterEvent(req *model.CreateEventRequest, projectId string, log *logger.FMLogger) {
	_, errMsg := s.CreateEvent(req)
	if errMsg != nil {
		log.Error("create new event [%+v] service error:%+v", req, errMsg)
		return
	}
}

func (s *EventService) BuildAndRegisterEvent(name, resId, detail, msg, state string) {
	registerReq := &model.CreateEventRequest{
		Name:         name,
		ResourceType: common.EventResourceTypeInstance,
		TraceService: common.EventResourceTraceService,
		ResourceId:   resId,
		ResourceName: common.EventResourceTypeInstance,
		TraceState:   state,
		EventType:    common.EventTypeData,
		SourceIp:     utils.GetLocalIP(),
		Details:      detail,
		SmnMessage:   fmt.Sprintf("Have triggerred an %s event: Name: %s, Abstract:%s", state, name, msg),
	}
	_, errMsg := s.CreateEvent(registerReq)
	if errMsg != nil {
		logger.R.Error("create new event [%+v] service error:%+v", registerReq, errMsg)
		return
	}
}
